<template>
<div>
  <!--     面包屑导航区域-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>权限管理</el-breadcrumb-item>
    <el-breadcrumb-item>角色列表</el-breadcrumb-item>
  </el-breadcrumb>
<!--  卡片区域-->
  <el-card>
    <el-row>
      <el-col>
        <el-button type="primary">添加角色</el-button>
      </el-col>
    </el-row>
    <el-table :data="roleList" border stripe>
<!--      展开列-->
      <el-table-column  type="expand">
        <template  slot-scope="scope">
          <el-row :class="['bdbottom',i1 === 0?'bdtop':'','vc-center']"  v-for="(item1, i1) in scope.row.children" :key="i1">
<!--            一级-->
            <el-col :span="5">
              <el-tag>{{item1.authName }}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
<!--            二级-->
            <el-col :span="19">
              <el-row :class="[i2 === 0?'':'bdtop','vc-center']" v-for="(item2, i2) in item1.children" :key="i2">
<!--                二级-->
                <el-col :span="10">
                  <el-tag type="success">{{item2.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
<!--                三级-->
                <el-col :span="14">
                  <el-tag  type="warning" @close="removeRightById(scope.row, item3.id)" closable v-for="(item3, i3) in item2.children" :key="i3" >
                    {{item3.authName}}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
<!--      索引-->
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作" width="300px" >
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
          <el-button size="mini" type="warning" @click="showSetRightDialog(scope.row)" icon="el-icon-setting">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
<!--  对话框-->
  <el-dialog
    title="提示"
    :visible.sync="setDialogVisible"
    width="50%"
    :before-close="handleClose"
    @close="setRightDialogClose"
  >
<!--    树形空间-->
    <el-tree :default-checked-keys="defKeys" node-key="id" default-expand-all show-checkbox :data="tree" :props="defaultProps" ></el-tree>
    <span slot="footer" class="dialog-footer">
    <el-button @click="setDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="setDialogVisible = false">确 定</el-button>
  </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: 'index',
  created () {
    this.getRolesList()
  },
  data () {
    return {
      roleList: [],
      // 对话框展示
      setDialogVisible: false,
      tree: [],
      // 后者为这个属性绑定的是数据的哪个属性
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: []
    }
  },
  methods: {
    getRolesList () {
      const data = [
        {
          roleDesc: '技术负责人',
          roleName: '主管',
          id: 30,
          children: [
            {
              id: 101,
              authName: '商品列表',
              path: 'goods',
              children: [
                {
                  id: 111,
                  authName: '商品列表',
                  path: 'goods',
                  children: [
                    {
                      id: 1111,
                      authName: '商品修改',
                      path: 'goods'
                    },
                    {
                      id: 1112,
                      authName: '商品',
                      path: 'goods'
                    }
                  ]
                },
                {
                  id: 112,
                  authName: '商品分类',
                  path: 'goods',
                  children: [
                    {
                      id: 1121,
                      authName: '分类修改',
                      path: 'goods'
                    }
                  ]
                }
              ]
            },
            {
              id: 102,
              authName: '订单列表',
              path: 'goods',
              children: [
                {
                  id: 121,
                  authName: '品列表',
                  path: 'goods',
                  children: [
                    {
                      id: 120,
                      authName: '商列表',
                      path: 'goods'
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          roleDesc: '测试',
          roleName: '测试1',
          id: 30,
          children: []
        }
      ]
      this.roleList = data
    },
    async removeRightById (role, rightId) {
      // 弹框提示用户
      const confirmResult = await this.$confirm('此操作将删除此权限', '是否继续此操作', '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消删除')
      } else {
        console.log(role.id)
        console.log(rightId)
        // 删除这里有个获取返回data然后重新赋值为res
        return this.$message.success('确认删除')
        // 删除后重新获取
        // eslint-disable-next-line no-unreachable
        // this.getRolesList()
        // role.children = res.data
      }
    },
    showSetRightDialog (role) {
      // 获取所有权限数据
      const data = [
        {
          authName: '商品分类',
          id: 1,
          path: '',
          pid: '',
          children: [
            {
              authName: '商品类',
              id: 101,
              path: '',
              pid: '',
              children: [
                {
                  authName: '商品分',
                  id: 1111,
                  path: '',
                  pid: ''
                },
                {
                  authName: '商品分',
                  id: 1112,
                  path: '',
                  pid: ''
                },
                {
                  authName: '修改',
                  id: 1113,
                  path: '',
                  pid: ''
                }
              ]
            }
          ]
        }
      ]
      this.tree = data
      // 递归
      this.getLeafKeys(role, this.defKeys)
      // 分配权限的对话框
      this.setDialogVisible = true
    },
    handleClose (done) {
      done()
    },
    // 通过递归的形式，获取当前角色的所有三级权限的id，并保存到defKeys数组中
    getLeafKeys (node, arr) {
      // 如果当前node节点不包含children属性，则是三级节点
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => this.getLeafKeys(item, arr))
    },
    // 监听分配权限对话框的关闭时间
    setRightDialogClose () {
      // 防止数组值的冲突
      this.defKeys = []
    }
  }
}
</script>

<style scoped>
.el-tag{
  margin: 7px;
}
  .bdtop{
    border-top:1px solid #eee ;
  }
  .bdbottom{
    border-bottom: 1px solid #eee;
    width: 90%;
    margin: 0 auto;
  }
  .vc-center{
    display: flex;
    align-items: center;
  }
</style>
